package com.springboot.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.springboot.blog.beans.BlogLog;
import com.springboot.blog.beans.PageBlog;
import com.springboot.blog.beans.YouQing;
import com.springboot.blog.config.utils.R;
import com.springboot.blog.dao.LogDao;
import com.springboot.blog.service.LogService;
import net.sf.json.JSONArray;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class LogController {

    @Resource
    LogDao logDao;
    @Resource
    private LogService logService;

    @RequestMapping("allLog")
    public String allLog(Integer page,Integer limit){
        PageBlog<BlogLog> blogLogPageBlog = logService.pageLog(page, limit);
        Integer logcount = logDao.selectCount(null);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("count",logcount);
        jsonObject.put("data",blogLogPageBlog);
        return jsonObject.toString();
    }
    @RequestMapping("deleteLog")
    public R deleteBlog(String delete){
        List<BlogLog> list2=(List<BlogLog>) JSONArray.toList(JSONArray.fromObject(delete), BlogLog.class);
        List<Integer> userList = new ArrayList<>();
        for (BlogLog blogLog : list2){
            userList.add(blogLog.getLogid());
        }
        int deleteUser = logDao.deleteBatchIds(userList);
        if (deleteUser>0) {
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("deleteByIdLog")
    public R deleteClassify(String logid){
        int delete = logDao.deleteById(Integer.parseInt(logid));
        if (delete>0){
            return R.ok();
        }
        return R.error();
    }
}
